package ar.edu.itba.pod.legajo48062;

import java.io.Serializable;
import java.rmi.RemoteException;

import ar.edu.itba.event.EventInformation;
import ar.edu.itba.node.api.ClusterAdministration;
import ar.edu.itba.pod.agent.runner.Agent;
import ar.edu.itba.pod.multithread.MultiThreadEventDispatcher;

public class WrapperEventDispatcher extends MultiThreadEventDispatcher {
	private DistributedEventDispatcher remoteDispatcher;
	private Cluster cluster;
	
	public WrapperEventDispatcher(ClusterAdministration cluster) {
		super();
		this.cluster=(Cluster) cluster;
	}
	
	public void setRemoteDispatcher(DistributedEventDispatcher remoteDispatcher){
		this.remoteDispatcher=remoteDispatcher;
	}

	@Override
	public void publish(Agent source, Serializable event) throws InterruptedException {		
		EventInformation e = new EventInformation(event, cluster.me().id(), source);
		e.setReceivedTime(Host.getTime());
		try {
			remoteDispatcher.publish(e);
		} catch (RemoteException e1) {
			e1.printStackTrace();
		}
	}
	
	public void publish(EventInformation event) throws InterruptedException {
		super.publish(event.source(), event.event());	
	}
}
